package com.wzdigit.wms.hm.utils;

import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

public class StringUtils {

	/**
	 * 判断字符串是否为空
	 * 
	 * @param String
	 *            str
	 * @return boolean
	 */
	public final static boolean isNull(String str) {
		return str == null || str.trim().isEmpty() || "null".equalsIgnoreCase(str.trim()) || "".equals(str.trim());
	}

	public final static boolean isNotNull(String str) {
		return !StringUtils.isNull(str);
	}

	public final static boolean isNotNulls(String... strs) {
		for (String str : strs) {
			if (StringUtils.isNull(str))
				return false;
		}
		return true;
	}

	public final static boolean isNulls(String... strs) {
		for (String str : strs) {
			if (StringUtils.isNotNull(str))
				return false;
		}
		return true;
	}

	/**
	 * 判断是否为整数
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isInteger(String str) {
		Pattern pattern = Pattern.compile("^[+]?[\\d]*$");
		return pattern.matcher(str).matches();
	}

	/**
	 * 判断是否为数值
	 * 
	 * @param str
	 * @return
	 */
	public static boolean isNumber(String str) {
		// ?:0或1个, *:0或多个, +:1或多个
		Pattern pattern = Pattern.compile("^[+]?[\\d]+.?[\\d]*$");
		return pattern.matcher(str).matches();
	}

	/**
	 * 字符串内容转换驼峰规则
	 * @param str
	 * @return
	 */
	public static String strTransformation(String str) {
		String result = str;
		if (str.contains("_")) {
			StringBuffer sb = new StringBuffer();
			Pattern pattern = Pattern.compile("([A-Za-z\\d]+)(_)?");
			Matcher matcher = pattern.matcher(str);
			boolean smallCamel = true;
			while (matcher.find()) {
				String word = matcher.group();
				sb.append(smallCamel && matcher.start() == 0 ? Character.toLowerCase(word.charAt(0))
						: Character.toUpperCase(word.charAt(0)));
				int index = word.lastIndexOf('_');
				if (index > 0) {
					sb.append(word.substring(1, index).toLowerCase());
				} else {
					sb.append(word.substring(1).toLowerCase());
				}
			}
			result = sb.toString();
		} else {
			result = str.toLowerCase();
		}
		return result;
	}
	
	public static String decode(String str) {
		try {
			return URLDecoder.decode(str, "UTF-8");
		} catch (Exception e) {
			return str;
		}
	}
	
	public static String encode(String str) {
		try {
			return URLEncoder.encode(str, "UTF-8");
		} catch (Exception e) {
			return str;
		}
	}
	
	public static List<String> split(String str, String separator) {
		if(isNull(str)) {
			return null;
		} else {
			return Arrays.asList(str.split(separator)).stream().collect(Collectors.toList());
		}
	}
}